/*
The MIT License (MIT)

Copyright (c) 2014 Voda.io

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef VODA_SCRIPT_H_IMPL
#define VODA_SCRIPT_H_IMPL

#pragma once
#include "runtime.h"
#include "context.h"
#include "globals.h"

namespace voda
{
	/**
	* \brief	forward declearation
	* \detail	
	*/
	class application;

	/**
	* \brief	script
	* \detail	
	*/
	class script
		: public iscript
		, public boost::enable_shared_from_this<script>
	{
		boost::shared_ptr<application> const app_;
		boost::asio::io_service & io_service_;
		boost::asio::io_service::strand strand_;

		runtime const & rt_;
		context const cx_;
		globals const gl_;

		script (boost::shared_ptr<application> const & app);
		static void on_error (JSContext *cx, char const * message, JSErrorReport *report);

		/**
		* \brief	Include
		* \detail	Loads files, and executes them
		*/
		static JSBool Include (JSContext *cx, unsigned argc, jsval *vp);

		/**
		* \brief	IncludeAsync
		* \detail	Loads file, and execute it async
		*/
		static JSBool IncludeAsync (JSContext *cx, unsigned argc, jsval *vp);

		/**
		* \brief	eval
		* \detail	Evaluate code
		*/
		static JSBool Eval (JSContext *cx, unsigned argc, jsval *vp);

		/**
		* \brief	eval_async
		* \detail	Evaluate code asyncronous
		*/
		static JSBool EvalAsync (JSContext *cx, unsigned argc, jsval *vp);

		/**
		* \brief	GC
		* \detail	collect the garbage
		*/
		static JSBool GC (JSContext *cx, unsigned /*argc*/, jsval *vp);

		/**
		* \brief	call_handler
		* \detail	helper
		*/
		void call_handler (boost::function<void()> const & fn);

		/**
		* \brief	unlocked_include
		* \detail	helper
		*/
		boost::any unlocked_include (std::string const & file, bool jit = true);

		/**
		* \brief	unlocked_eval
		* \detail	helper
		*/
		boost::any unlocked_eval (std::string const & text, bool jit = false);

		/**
		* \brief	include_async_call
		* \detail	helper
		*/
		void on_include_async(JSContext *cx, JS::Heap<JSString*> & path, JSBool jit, JS::Heap<JSObject*> & callback);

		/**
		* \brief	on_eval_async
		* \detail	helper
		*/
		void on_eval_async(JSContext *cx, JS::Heap<JSString*> & text, JSBool jit, JS::Heap<JSObject*> & callback);

	public:

		static boost::shared_ptr<script> create (boost::shared_ptr<application> const & app, boost::function<void(script*)> const & deleter);
		virtual ~script (void);

		//	iasync
		virtual void post (boost::function<void()> const & handler);
		virtual void io_post (boost::function<void()> const & handler);
		virtual boost::asio::io_service & get_io_service ();
		virtual boost::asio::io_service::strand & get_strand ();

		//	iscript
		virtual boost::any include (std::string const & file, bool jit = true);
		virtual void include_async (std::string const & file, bool jit, boost::function<void(boost::any const &)> const & callback);
		virtual boost::any eval (std::string const & text, bool jit = false);
		virtual void eval_async (std::string const & text, bool jit, boost::function<void(boost::any const &)> const & callback);

		//	helpers
		runtime const & get_runtime () const throw () { return rt_;}
		context const & get_context () const throw () { return cx_;}
		globals const & get_globals () const throw () { return gl_;}

		//	script locker
		class lock
		{
			runtime::lock const rlock_;
			context::lock const clock_;
			globals::lock const glocl_;

		public:
			lock (script *pscript)
				: rlock_ (pscript->get_runtime())
				, clock_ (pscript->get_context())
				, glocl_ (pscript->get_context(), pscript->get_globals())
			{
			}

			~lock()
			{
			}
		};
	};

}//namespace voda

#endif//VODA_SCRIPT_H_IMPL